package com.say.utils;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ResourceUtils;

import com.say.framework.idworker.IdWorker;




/**
 * 
 * 类名称：ImageTools<br>
 * 类描述：图片工具类<br>
 * @version v1.0
 *
 */
public class ImageTools {
    protected static Logger logger = LoggerFactory.getLogger(ImageTools.class);
    /**
     * jpg后缀
     */
    public static final String SUFF_JPG = ".jpg";

    /**
     * png后缀
     */
    public static final String SUFF_PNG = ".png";

    /**
     * gif后缀
     */
    public static final String SUFF_GIF = ".gif";

    /**
     * 保存来自base64的图片,返回参数不为空保存成功
     * @param base64String
     * @return
     */
    public static String savePic4Base64(String base64String) {
        String saveFileName = IdWorker.generateId() + getPicExtension(base64String);
        //判断是否为base64字符串，不是要处理
        if (!Base64.isBase64(base64String)) {
            String[] base64Strs = base64String.split(",");
            if (base64Strs.length > 1) {
                base64String = base64Strs[1];
            }
        }
        byte[] bytes = Base64.decodeBase64(base64String);
        for (int i = 0; i < bytes.length; ++i) {
            if (bytes[i] < 0) {//调整异常数据  
                bytes[i] += 256;
            }
        }
    	File path = null;
 	    try {
 	    	path = new File(ResourceUtils.getURL("classpath:").getPath());
 	    } catch (FileNotFoundException e) {
 	    	e.printStackTrace();
 	    }
    	String savePath=SysParentFile.getParentFile();
    	
        String date = DateHelper.formatDateByFormat(new Date(), "yyyy/MM/dd");
        // 创建文件夹
        File f = new File(savePath+date);
        if (!f.exists()) {
            f.mkdirs();
        }
        String filePath= date + "/"+saveFileName;
        String allPath = savePath + filePath;//图片保存路径
        try {
            IOUtils.write(bytes, new FileOutputStream(allPath));
        } catch (IOException e) {
            e.printStackTrace();
            allPath = null;
        }
        return filePath;
    }

    
    /**
     * 从base64字符串中获取图片后缀
     * @return
     */
    private static String getPicExtension(String base64String) {
        //默认使用jpg
        if (base64String.contains("image/jpg")) {
            return SUFF_JPG;
        } else if (base64String.contains("image/png")) {
            return SUFF_PNG;
        } else if (base64String.contains("image/gif")) {
            return SUFF_GIF;
        } else {
            return SUFF_JPG;
        }
    }

    /**
     * 获取图片宽度
     * @param file 图片文件
     * @return 宽度
     */
    public static int getImgWidth(File file) {
        InputStream is = null;
        BufferedImage src = null;
        int ret = -1;
        try {
            is = new FileInputStream(file);
            src = javax.imageio.ImageIO.read(is);
            ret = src.getWidth(null); // 得到源图宽
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ret;
    }

    /**
     * 获取图片高度
     * @param file 图片文件
     * @return 高度
     */
    public static int getImgHeight(File file) {
        InputStream is = null;
        BufferedImage src = null;
        int ret = -1;
        try {
            is = new FileInputStream(file);
            src = javax.imageio.ImageIO.read(is);
            ret = src.getHeight(null); // 得到源图高
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ret;
    }
}
